package cn.qingsec.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaCheckRole;
import cn.dev33.satoken.annotation.SaMode;
import cn.dev33.satoken.stp.StpUtil;
import cn.dev33.satoken.util.SaResult;
import cn.qingsec.service.Oauth2ClientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("check")
public class CheckPermissionController {
    @Autowired
    private Oauth2ClientService oauth2ClientService;
    // 登录认证，登录之后才可以进入方法

    @SaCheckLogin
    @GetMapping("checkLogin")
    public SaResult checkLogin() {
        System.out.println();
        System.out.println("--------------进入请求--------------");
//        System.out.println(StpUtil.getExtra("username"));
//        System.out.println(StpUtil.getExtra("nick"));
        return SaResult.ok();
    }
    // 权限认证，具备user-add权限才可以进入方法  ---- http://localhost:8081/at/checkPermission
    @SaCheckPermission("user.add")
    @GetMapping("checkPermission")
    public SaResult checkPermission() {
        System.out.println("--------------checkPermission--------------");
        return SaResult.ok();
    }

    // 权限认证，同时具备所有权限才可以进入  ---- http://localhost:8081/at/checkPermissionAnd
    @SaCheckPermission({"user.add", "user.delete", "user.update"})
    @GetMapping("checkPermissionAnd")
    public SaResult checkPermissionAnd() {
        return SaResult.ok();
    }

    // 权限认证，只要具备其中一个就可以进入  ---- http://localhost:8081/at/checkPermissionOr
    @SaCheckPermission(value = {"user.add", "user.delete", "user.update"}, mode = SaMode.OR)
    @GetMapping("checkPermissionOr")
    public SaResult checkPermissionOr() {
        return SaResult.ok();
    }

    // 角色认证，只有具备admin角色才可以进入  ---- http://localhost:8081/at/checkRole
    @SaCheckRole("admin")
    @GetMapping("checkRole")
    public SaResult checkRole() {
        return SaResult.ok();
    }

    // 完成二级认证  ---- http://localhost:8081/at/openSafe
    @GetMapping("openSafe")
    public SaResult openSafe() {
        StpUtil.openSafe(200); // 打开二级认证，有效期为200秒
        return SaResult.ok();
    }

    @GetMapping("test")
    public SaResult test(){
        return oauth2ClientService.test();
    }
}
